package com.imooc.mall.controller;

import com.imooc.mall.common.ApiRestResponse;
import com.imooc.mall.common.Constant;
import com.imooc.mall.exception.ImoocMallException;
import com.imooc.mall.exception.ImoocMallExceptionEnum;
import com.imooc.mall.model.pojo.User;
import com.imooc.mall.service.UserService;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
public class UserController {

    @Resource
    UserService userService;

    @GetMapping("/test")
    @ResponseBody
    public User personalPage() {
        User user = userService.getUser();
        return user;
    }

    @PostMapping("/register")
    @ResponseBody
    public ApiRestResponse register(@RequestParam("username") String username, @RequestParam("password") String password) {
        if (StringUtils.isEmpty(username)) {
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_USER_NAME);
        }
        if (StringUtils.isEmpty(password)) {
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_PASSWORD);
        }

        if (password.length() < 8) {
            return ApiRestResponse.error(ImoocMallExceptionEnum.PASSWORD_TOO_SHORT);
        }

        try {
            userService.register(username, password);
            return ApiRestResponse.success();
        } catch (ImoocMallException ex) {
            ex.printStackTrace();
            return ApiRestResponse.error(ex.getCode(),ex.getMsg());
        }

    }
    @PostMapping("/login")
    @ResponseBody
    public ApiRestResponse login(@RequestParam("username") String username,
      @RequestParam("password") String password, HttpSession session){
        if (StringUtils.isEmpty(username)) {
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_USER_NAME);
        }
        if (StringUtils.isEmpty(password)) {
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_PASSWORD);
        }
        try {
            User user = userService.login(username, password);
            user.setPassword(null);
            session.setAttribute(Constant.IMOOC_MALL_USER,user);

            return ApiRestResponse.success(user);

        } catch (ImoocMallException e) {
            e.printStackTrace();
            return ApiRestResponse.error(e.getCode(),e.getMsg());
        }

    }


    @PostMapping("/user/update")
    @ResponseBody
    public ApiRestResponse updateUserInfo(HttpSession session,@RequestParam("signature")String signature){
        User userCurrent =(User) session.getAttribute(Constant.IMOOC_MALL_USER);
        if(userCurrent==null){
            return  ApiRestResponse.error(ImoocMallExceptionEnum.NEED_LOGIN);
        }
        User user = new User();
        user.setId(userCurrent.getId());
        user.setPersonalizedSignature(signature);
        try {
            userService.updateInformation(user);
            return ApiRestResponse.success();

        } catch (ImoocMallException e) {
            e.printStackTrace();
            return ApiRestResponse.error(e.getCode(),e.getMsg());
        }

    }

    @PostMapping("/user/logout")
    @ResponseBody
    public ApiRestResponse logout(HttpSession session){
        session.removeAttribute(Constant.IMOOC_MALL_USER);
        return ApiRestResponse.success();
    }


    @PostMapping("/adminLogin")
    @ResponseBody
    public ApiRestResponse adminLogin(@RequestParam("username") String username,
                                 @RequestParam("password") String password, HttpSession session){
        if (StringUtils.isEmpty(username)) {
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_USER_NAME);
        }
        if (StringUtils.isEmpty(password)) {
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_PASSWORD);
        }
        try {
            User user = userService.login(username, password);
            //校验是否是管理员
            boolean b = userService.checkAdminRole(user);
            if(b){
                user.setPassword(null);
                session.setAttribute(Constant.IMOOC_MALL_USER,user);
                return ApiRestResponse.success(user);
            }else{
                return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_ADMIN);
            }


        } catch (ImoocMallException e) {
            e.printStackTrace();
            return ApiRestResponse.error(e.getCode(),e.getMsg());
        }

    }

}
